/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import system.*;
import java.awt.event.*;

/**
 *
 * @author Thommie
 */
public class logInFrame extends JFrame implements ActionListener {

    UserHandler us;
    String empty;
    String invalidID;
    JLabel label;
    JTextField textfield;
    JPasswordField passwordfield;
    JButton jb;
    Container c;
    JPanel ctop;
    JPanel cbot;
    private boolean continueValue;
    private javax.swing.JProgressBar pBar;

    public logInFrame(UserHandler uH) {
        us = uH;
        pBar = new JProgressBar();
        empty = "";
        label = new JLabel();
        ctop = new JPanel();
        cbot = new JPanel();
        textfield = new JTextField("Enter Username");
        passwordfield = new JPasswordField("Enter Pimpword");
        invalidID = "Invalid LogIn                                  ";
        label.setForeground(Color.red);

        textfield.setSelectionColor(Color.GREEN);
        textfield.setToolTipText("Enter Username Here");
        passwordfield.setSelectionColor(Color.CYAN);
        passwordfield.addFocusListener(new java.awt.event.FocusAdapter() {

           
            public void focusGained(java.awt.event.FocusEvent ev) {
                if (passwordfield.getText().equals("Enter Pimpword")) {
                    passwordfield.selectAll();
                }
            }
        });

        textfield.addFocusListener(new java.awt.event.FocusAdapter() {

            
            public void focusGained(java.awt.event.FocusEvent evt) {
                if (textfield.getText().equals("Enter Username")) {
                    textfield.selectAll();
                }
            }
        });
        textfield.setColumns(10);
        passwordfield.setColumns(10);


        pBar.setIndeterminate(true);
        pBar.setVisible(false);
        //pBar.setMaximum(100);
        //pBar.setValue(100);

        continueValue = true;
        c = getContentPane();
        c.setLayout(new BorderLayout());
        jb = new JButton("Login");

        c.add(ctop, BorderLayout.CENTER);
        c.add(cbot, BorderLayout.SOUTH);

        ctop.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 5));

        ctop.setBorder(BorderFactory.createEmptyBorder(30, 30, 0, 0)); // uppe,vänster,nere,höger

        ctop.add(textfield);
        ctop.add(jb);
        ctop.add(passwordfield);
        ctop.add(label);

        cbot.setLayout(new FlowLayout(FlowLayout.RIGHT));
        cbot.add(pBar);
        cbot.setBackground(Color.red);
        ctop.setBackground(Color.darkGray);


        jb.addActionListener(this);
        jb.setMnemonic(KeyEvent.VK_ENTER);

        setTitle("Log in");
        setSize(300, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.setLocation(470, 250);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jb) {
            if (us.login(textfield.getText(),passwordfield.getText())) {
                label.setText(empty);
                pBar.setVisible(true);
                setContinueValue(false);

            } else {
                textfield.setText("");
                passwordfield.setText("");
                label.setText(invalidID);
                textfield.requestFocusInWindow();
            }

        }
    }

    /**
     * @return the continueValue
     */
    public boolean isContinueValue() {
        return continueValue;
    }

    /**
     * @param continueValue the continueValue to set
     */
    public void setContinueValue(boolean continueValue) {
        this.continueValue = continueValue;
    }
}
